//
//  NewSongTableViewCell.m
//  22-AliPlanet_HomeWork
//
//  Created by 曹曹 on 16/8/13.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "NewSongTableViewCell.h"
#import "LittleData.h"
#import "NewSongCollecCell.h"
#import "UIImageView+DownloadImage.h"

@interface NewSongTableViewCell () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@end

@implementation NewSongTableViewCell
//- (void)layoutSubviews {
//    [super layoutSubviews];
//    _collectionView.delegate = self;
//    _collectionView.dataSource = self;
//    CGFloat width = (self.contentView.frame.size.width - 20) / 3;
//    CGFloat height = width * 3 / 2;
//    _layout.itemSize = CGSizeMake(width, height);
//    if (_data) {
//        [self.collectionView reloadData];
//    }
//}

- (void)setData:(NSArray<LittleData *> *)data {
    _data = data;
    [_collectionView reloadData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _data.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NewSongCollecCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"newsong" forIndexPath:indexPath];
    cell.nameLabel.text = _data[indexPath.row].name;
    cell.descLabel.text = _data[indexPath.row].desc;
    [cell.image asyncDownloadImageWithURL:_data[indexPath.row].picUrl completionHandler:^(NSData *data, UIImage *image) {}];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat margin = 5;
    CGFloat insect = 10;
    CGFloat width = (CGRectGetWidth(collectionView.bounds) - margin * 2 - insect * 2) / 3;
    return CGSizeMake(width, width * 4 / 3);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%ld, %ld", indexPath.row, indexPath.section);
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
